package com.neteast.androidclient.newscenter.domain;

import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.HashSet;

public class KeepLive {

	public byte cmd;
	/** 通信协议 */
	public int protocolVersion;
	/** 报文id*/
	public int msgId;
	/** 用户id*/
	public int userId;
	/** 广播id*/
	public long broadcastId;
	/**单播id*/
	public long unicastId;
	/** user_optinfo的长度*/
	public int userOptinfoLen;
	/** 在两次心跳之间用户点击了哪些消息*/
	public HashSet<Long> userOptinfo;
	/** 保留字段长度 */
	public int reserveLen;
	/** 保留字段 */
	public byte[] reserve;
	
	
	/**
	 * 
	 * @param cmd 标志位 byte
	 * @param protocolVersion 通信协议 int
	 * @param msgId 报文id int
	 * @param userId 用户id
	 * @param broadcastId 最后的一次广播id
	 * @param unicastId 最后的一次单播id
	 * @param userOptinfoLen 用户操作的长度 8的倍数 int
	 * @param userOptinfo 用户点击的消息id的数组 long[]
	 * @param reserveLen 保留字段长度 int
	 * @param reserve 保留字段 byte[]
	 */
	public KeepLive(byte cmd, int protocolVersion, int msgId, int userId,
			long broadcastId, long unicastId, int userOptinfoLen,
			HashSet<Long> userOptinfo, int reserveLen, byte[] reserve) {
		super();
		this.cmd = cmd;
		this.protocolVersion = protocolVersion;
		this.msgId = msgId;
		this.userId = userId;
		this.broadcastId = broadcastId;
		this.unicastId = unicastId;
		this.userOptinfoLen = userOptinfoLen;
		this.userOptinfo = userOptinfo;
		this.reserveLen = reserveLen;
		this.reserve = reserve;
	}
	
	
	/**
	 * 
	 * @param cmd 标志位 byte
	 * @param protocolVersion 通信协议 int
	 * @param msgId 报文id int
	 * @param userId 用户id
	 * @param broadcastId 最后的一次广播id
	 * @param unicastId 最后的一次单播id
	 * @param userOptinfoLen 用户操作的长度 8的倍数 int
	 * @param userOptinfo 用户点击的消息id的数组 long[]
	 */
	public KeepLive(byte cmd, int protocolVersion, int msgId, int userId,
			long broadcastId, long unicastId, int userOptinfoLen,
			HashSet<Long> userOptinfo) {
		super();
		this.cmd = cmd;
		this.protocolVersion = protocolVersion;
		this.msgId = msgId;
		this.userId = userId;
		this.broadcastId = broadcastId;
		this.unicastId = unicastId;
		this.userOptinfoLen = userOptinfoLen;
		this.userOptinfo = userOptinfo;
		this.reserveLen=0;
	}



	public byte[] getMessage() {
		ByteBuffer buffer=ByteBuffer.allocate(37+userOptinfoLen+reserveLen);
		buffer.put(cmd);
		buffer.putInt(protocolVersion);
		buffer.putInt(msgId);
		buffer.putInt(userId);
		buffer.putLong(broadcastId);
		buffer.putLong(unicastId);
		buffer.putInt(userOptinfoLen);
		if (userOptinfoLen>0) {
			for (Long i:userOptinfo) {
				buffer.putLong(i);
			}
		}
		buffer.putInt(reserveLen);
		if (reserveLen>0) {
			buffer.put(reserve);
		}
		buffer.flip();
		return buffer.array();
	}


	@Override
	public String toString() {
		if (reserveLen>0) {
			return "KeepLive [cmd=" + cmd + ", protocolVersion=" + protocolVersion
					+ ", msgId=" + msgId + ", userId=" + userId + ", broadcastId="
					+ broadcastId + ", unicastId=" + unicastId
					+ ", userOptinfoLen=" + userOptinfoLen + ", userOptinfo="
					+ userOptinfo + ", reserveLen=" + reserveLen
					+ ", reserve=" + Arrays.toString(reserve) + "]";
		} else {
			return "KeepLive [cmd=" + cmd + ", protocolVersion=" + protocolVersion
					+ ", msgId=" + msgId + ", userId=" + userId + ", broadcastId="
					+ broadcastId + ", unicastId=" + unicastId
					+ ", userOptinfoLen=" + userOptinfoLen + ", userOptinfo="
					+ userOptinfo + ", reserveLen=" +reserveLen+ "]";
		}
	}
}
